<?php
/**
 * Yabsta News
 *
 * This file is a subject under proprietary sofrware license
 * 
 * @category
 * @package
 * @subpackage
 * @author      valerii lazarevych <lazarevuch@gmail.com>
 * @copyright   Copyright(c) Valerii Lazarevych
 * @license     proprietary software license
 * @version     $Id: IndexController.php 26.05.2012 18:31:33 valerii $
 */

/**
 * DomainObject Abscract Class
 * 
 * Base for all application data objects
 * according to domain object oop design pattern
 */
abstract class domainObject
{    
    /**
     * Instantiated domain objects total
     * 
     * @var int $idcount
     */
    static protected $objCount             =   0;
    
    /**
     * Domain object identification number in app stack
     * 
     * @var int $oId
     */
    protected   $oid                      =   null;
    
    /**
     * Object id in database
     * 
     * @var int $id
     */
    protected   $id                       =   0;  
    
    /**
     * ORM model for own domain object
     * to mapping own to db
     * 
     * @todo go to static private access base
     * 
     * @var Model $dbModel
     */
    private     $dbModel                  =   null;
    
    /**
     * Own relation mapping object class name suffix
     * 
     * @var string $modelNameSuffix
     */
    private     $modelNameSuffix          =  's';
    
    
     /**
     * Constructor
     * 
     * Increment total id count
     * Set current object id
     * Set ORM object
      * 
      * @param Model $objectModel
     */
    public function __construct( Model $objectModel = null)
    {
        self::$objCount++;
        
        $this->oid      =   self::$objCount;
        
        $nameParts      =   explode('_', get_class($this));
        $modelName      =   'models_'.$nameParts[1];                               
        
        //if ( self::$dbModel === null)
        $this->dbModel  =  new $modelName;                
    }
    
    /**
     * Get object idenfication number
     * 
     * @return int
     */
    public function getObjectId()
    {
        return $this->oid;
    }
    
    /**
     * Get obj database Id    
     * 
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
    
    /**
     * Set obj database id
     *  
     * @param int $id 
     */
    public function setId($id)
    {
        $this->id   =   (int)$id;
    }


    /**
     * Find obj in database by id         
     * 
     * @return bool
     */
    public function find($id = null)
    {
        if ( !$this->getId() && $id)
              $this->setId( (int)$id );
        
        $this->dbModel->find($this);        
                
        return  (bool)$this->getId();
    }        
    
    /**
     * Save obj to database
     * 
     * @return bool
     */
    public function save()
    {                       
        return $this->getId() ? $this->dbModel->update($this) : $this->dbModel->insert($this);        
    }
    
    /**
     * Delete obj from database
     * 
     * @return bool
     */
    public function remove()
    {
        return $this->getId()   ?   $this->dbModel->remove($this)   :   false;
    }
    
    /**
     * Build domain object based on array of properties
     * 
     * @param array $props
     * @return domainObject inheritor
     */
    public function build ( array $props )
    {                       
        foreach( get_object_vars($this) as $propName => $propValue) {                        
            
            $this->{$propName}  =  isset($props[$propName]) && $props[$propName] != ''  
                                       ? $props[$propName]  :  $propValue;        };
                                   
        return  $this;
    }
}

?>
